<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑商户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="myForm">
    <input type="hidden" name="id" id="id">
    <div class="layui-form-item">
        <label class="layui-form-label required">权限名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="" placeholder="请输入权限名称" value=""
                   class="layui-input">
        </div>
    </div>
    <input type="hidden" id="pid" name="pid">
    <input type="hidden" id="ppid" name="ppid">
    <div class="layui-form-item">
        <label class="layui-form-label required">所属菜单</label>
        <div class="layui-input-block">
            <input type="text" id="inputid" name="" lay-verify="required" lay-reqtext="" placeholder="请选择所属菜单"
                   value="" class="layui-input" readonly >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">权限地址</label>
        <div class="layui-input-block">
            <input type="text" name="url"  lay-verify="required" lay-reqtext="" placeholder="请输入权限地址"
                   value="javascript:void(0);" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <input type="text" name="description" placeholder="请输入描述" value="" class="layui-input" >
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="../../lib/layui/layui.js" charset="utf-8"></script>
<script src="../../js/provider/layutil.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
            var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
            //监听提交
        //获取全部的菜单
        var list=[];

        //显示当前菜单
        var currentPid = $("#pid").val();
        if(currentPid != null && currentPid != undefined) {
            var url = "/channel/sys-menu/getone/" + String(currentPid);
            layutil.get(url,{}, function (res) {
                var body = res.body;
                if(body.code == "0") {
                    var title = body.data.title;
                    $("#inputid").val(title);
                }
            });
        }

        form.on('submit(saveBtn)', function (data) {
                var str = "确定编辑权限信息？";
                layer.confirm(str, {btn: ['确定', '取消'], title: "提示"}, function (index) {
                    if($("#ppid").val()=='1'||$("#ppid").val()=='0'){
                        layer.msg("父级菜单不能添加权限！");
                        return false;
                    }else{
                        $.ajax({
                            url: '/channel/sys-permission/update',
                            type: 'POST',
                            dataType: 'json',
                            data: data.field,
                            headers:{'Authorization':'Bearer '+ JSON.parse(localStorage.getItem('userdetail')).token},
                            success: function(data) {
                                layer.msg("编辑权限信息成功", {
                                    icon: 6,//成功的表情
                                    time: 1000 //1秒关闭（如果不配置，默认是3秒）
                                }, function () {
                                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(iframeIndex);
                                });

                            },
                            error:function (data) {
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            }
                        });
                    }

                });
                return false;
            });

        $(document).on('click','#inputid',function (obj) {
            var defaultMenu = {id: $("#pid").val()};
            layutil.menuSelector(defaultMenu, function (res) {
                $("#ppid").val(res.pid);
                $("#inputid").val(res.title);
                $("#pid").val(res.id);
            });
        });
        form.render();
    });
</script>
</body>
</html>